/**
 * Created by du on 2015/8/5.
 */

function addquerstionCtrl($scope,$http,$timeout){
    $scope.uploadInit = function () {
        var uploader = new plupload.Uploader({
            browse_button: 'uploader',
            url: "/index.php/Home/Image/upload.html",
//                url: "/upload.php",
            max_file_size: '3mb',
            filters: [
                {title: "Image files", extensions: "jpg,gif,png,jpeg"},
            ],
            rename: true,
            multi_selection: false
        });
        uploader.init();

        uploader.bind('FilesAdded', function (up, files) {
            up.start();
        });
//
        uploader.bind('UploadProgress', function (up, file) {
        });

        uploader.bind('FileUploaded', function (d, file, r) {



            $res = $.parseJSON(r.response);
            var html=$("#imglist").html();
            html+="<span class='random_cell add_mon'><img src='"+ $res.result.url+" ' /></span>";
            $("#imglist").html(html);
            //$("#uploader img").attr("src", $res.result.url);
            //$("#cover").val($res.result.url);
            //$scope.formData.cover = $res.result.url;
//                    $("#coverImg").val($res.result.id);
        });
    }
    $scope.uploadInit();
    $scope.submit=function(){
        var imgs="";
        var types="";

        var lastprize=$("#myprize").val();
        var select=$("#Prize").text();
        if(parseInt(select)>parseInt(lastprize)){
            alert("您的积分不足!")
            return false;
        }
        $("#imglist img").each(function(i,item){

            imgs=imgs+$(item).attr("src")+",";

        });
        imgs=imgs.substring(0,imgs.lastIndexOf(','));
        $("#typeids").find("img").each(function(j,jtem){

            if($(jtem).attr("add")=="true"){
                types=types+$(jtem).parent().attr("attr")+",";
            }
        });
        types=types.substring(0,types.lastIndexOf(','));

        $scope.formdata.photos=imgs;
        $scope.formdata.helptype=types;
        $scope.formdata.uid=$("#uid").val();
        $scope.formdata.uname=$("#uname").val();
        $scope.formdata.prize=parseInt($("#Prize").text());
        $http({ method: 'POST', url: "/index.php/Home/Index/addquersion.html", data:
            $.param($scope.formdata),headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (data) {

            if(data.status==1){
                $scope.message="添加成功";
               $("#showmessage").click();
                var timer = $timeout(
                    function() {

                       window.location.href="/index.php/Home/Index/quertionlist.html"
                    },
                    1000
                );

            }else{
                $scope.message="添加失败";
                $("#showmessage").click();
            }
        });

    };
}




/*问题列表*/
function querstionlistCtrl($scope,$http){
    $scope.searchmodel={};
    $scope.searchmodel.type=0;
    $scope.page = {};
    $scope.page.current = 1;
    $scope.searchmodel.page=1;
    $scope.searchmodel.pagesize=10;
    $(document).ready(function(){
        var range = 50;             //距下边界长度/单位px
        var elemt = 500;           //插入元素高度/单位px
        var maxnum = 20;            //设置加载最多次数
        var num = 1;
        var totalheight = 0;
        var main = $("#content");                     //主体元素
        $(window).scroll(function(){
            var srollPos = $(window).scrollTop();    //滚动条距顶部距离(页面超出窗口的高度)

            //console.log("滚动条到顶部的垂直高度: "+$(document).scrollTop());
            //console.log("页面的文档高度 ："+$(document).height());
            //console.log('浏览器的高度：'+$(window).height());

            totalheight = parseFloat($(window).height()) + parseFloat(srollPos);
            if(($(document).height()-range) <= totalheight  && num != maxnum) {
                $scope.searchmodel.pagesize+=10;
                $scope.loaddata();
                num++;
            }
        });
    });
    var page = $scope.page.current;

    $scope.prePage = function(){

        if(page >= 2){
            page = page - 1;
            $scope.searchmodel.page=page;
            $scope.loaddata(page);
        }else{
          //  $scope.questiondata = $scope.questiondata;
        }

    }

    $scope.nextPage = function(){

        if(page < $scope.yeshu){
            page = page + 1;
            $scope.searchmodel.page=page;
            $scope.loaddata(page);
        }else{
            //$scope.questiondata = $scope.questiondata;
        }

    }
    $scope.loaddata=function(){

        $http({ method: 'POST', url: "/index.php/Home/Index/GetQuertList.html",
            data: $.param($scope.searchmodel),headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (data) {
            if (data['status']==1) {
                //$scope.courses=data["result"];
                //$scope.pagePagination(data);
                ////分页按钮事件
                //$("#pagebar ul li:not('.disabled') a").click($scope.pageclick);
                $scope.questiondata=data["result"];
                $scope.total = data["Total"];
                $scope.size = data["Size"];
                $scope.yeshu = Math.ceil($scope.total/$scope.size);
            } else {
                $scope.questiondata=new Array();
                //$scope.loaddata();//失败重新请求
            }
        });
    }
    $scope.loaddata(1);



}


